package com.tmpt.controller.action;


import com.opensymphony.xwork2.ActionSupport;
import com.tmpt.dto.*;
import com.tmpt.freemarker.exec.FreemarkerGen;
import com.tmpt.others.singleuser.OnlineUserBindingListener;
import com.tmpt.service.IMenuService;
import com.tmpt.service.IOperationlogService;
import com.tmpt.service.ISecurityV2Service;
import com.tmpt.utils.JsonMap;
import com.tmpt.utils.StringUtils;
import com.tmpt.utils.WebUtils;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
public class menuAction extends ActionSupport{
	private Logger log = Logger.getLogger(this.getClass());

	private static final long serialVersionUID = 1L;
	private String hasDataLimit;
	private String type;
	private String sort;
	private String id;
	private String name;
	private String optType;
	private String url;
	private String iconClass;
	//Role
	private String roleJsonArray;
	private String roleName;
	private String roleId;
	//USER
	private String userId;
	private String selectedRoleNodes;
	private String keyRandom;

	private String sourceType;

	private String result;
	private List<Map<String,Object>> jsonArrayResult;
	private Map<String,Object> jsonObjectResult;

	@Resource private IOperationlogService operationlogService;
	@Resource private IMenuService menuService;
	private A1OperationlogDto oprDtoBack = new A1OperationlogDto(WebUtils.getRequest(), A1OperationlogDto.UserTypeBack);


	/**
	 * 查出所有菜单结构
	 * @return
	 */
	public String menuTree4MenuUrls(){
		try{
			System.out.println("=======>id:"+id);
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();
			A2UserDto userDto = (A2UserDto)session.getAttribute("magSystemDtoSession");
			long userId = userDto.getId();
			System.out.println("=========>userId:" + userId);

			Map<String,Object> paramMap = new HashMap<String, Object>();
			paramMap.put("from","MenuMag");
			paramMap.put("id",id);
			Result resultObj = menuService.queryMenus4RoleConf(paramMap);
			if(!resultObj.isSuccess()){
				this.result = "数据加载失败";
				return "jsonResult";
			}

			this.jsonArrayResult = (List<Map<String,Object>>)resultObj.getResultMap().get("list");
			System.out.println("<=====jsonArrayResult:" + this.jsonArrayResult);

		}catch (Exception e) {
			this.result = "出错";
			e.printStackTrace();
		}
		return "jsonArray";
	}

	/**
	 * 查出所有菜单结构
	 * @return
	 */
	public String menuTree4JustMenu(){
		try{
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();
			A2UserDto userDto = (A2UserDto)session.getAttribute("magSystemDtoSession");
			long userId = userDto.getId();
			System.out.println("=========>userId" + userId);

			Map<String,Object> paramMap = new HashMap<String, Object>();
			paramMap.put("from","MenuFromRoleMag");
			paramMap.put("id", id);
			Result resultObj = menuService.queryMenus4RoleConf(paramMap);
			if(!resultObj.isSuccess()){
				this.result = "数据加载失败";
				return "jsonResult";
			}

			this.jsonArrayResult = (List<Map<String,Object>>)resultObj.getResultMap().get("list");
			System.out.println("<=====jsonArrayResult:" + this.jsonArrayResult);

		}catch (Exception e) {
			this.result = "出错";
			e.printStackTrace();
		}
		return "jsonArray";
	}

	/**
	 * 查出所有菜单结构
	 * @return
	 */
	public String menuTree4JustMenuParent(){
		try{
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();
			A2UserDto userDto = (A2UserDto)session.getAttribute("magSystemDtoSession");
			long userId = userDto.getId();
			System.out.println("=========>userId" + userId);

			Map<String,Object> paramMap = new HashMap<String, Object>();
			paramMap.put("id", id);
			Result resultObj = menuService.queryMenus4RoleConf(paramMap);
			if(!resultObj.isSuccess()){
				this.result = "数据加载失败";
				return "jsonResult";
			}

			this.jsonArrayResult = (List<Map<String,Object>>)resultObj.getResultMap().get("list");
			System.out.println("<=====jsonArrayResult:" + this.jsonArrayResult);

		}catch (Exception e) {
			this.result = "出错";
			e.printStackTrace();
		}
		return "jsonArray";
	}


	/**
	 * 用户菜单JSON
	 * @return
	 */
	public String queryMenusByUser(){
		HttpServletRequest request = WebUtils.getRequest();
		HttpSession session = request.getSession();
		A2UserDto userDto = (A2UserDto)session.getAttribute("magSystemDtoSession");
		long userId = userDto.getId();
		if(userDto == null){
			return "";
		}
		Result result = menuService.queryMenusByUser(userId);
		if(!result.isSuccess()){
			return "";
		}
		this.jsonArrayResult = (List<Map<String,Object>>)result.getResultMap().get("list");

		return "jsonArray";
	}


	public String addeditMenuNode(){
		Result resultObj = new Result();
		try{
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();
			A2UserDto userDto = (A2UserDto)session.getAttribute("magSystemDtoSession");
			long userId = userDto.getId();

			A3MenuDto menuDto = new A3MenuDto();
			menuDto.setText(name.trim());
			menuDto.setType(Integer.parseInt(type));
			menuDto.setUrl(url);
			menuDto.setSort(sort == null || sort.trim().equals("") ? 0 : Integer.parseInt(sort));
			menuDto.setIconClass(iconClass);
			menuDto.setHasDataLimit(Integer.parseInt(hasDataLimit));
			System.out.println("==========>ow_register_data==keyRandom:"+keyRandom);
			menuDto.setKeyRandom(keyRandom);
			if("add".equals(optType)){
				//添加
				long parantId = 0;//根节点
				parantId = Long.parseLong(id);
				resultObj = menuService.addMenu(menuDto, parantId);
			}else if("edit".equals(optType)){
				//修改
				menuDto.setId(Long.parseLong(id));
				resultObj = menuService.modifyMenuById(menuDto);
			}else{
				resultObj.fault("失败：没有相应操作类型");
			}

		}catch (Exception e) {
			resultObj.fault("出错:"+e.getLocalizedMessage());
			e.printStackTrace();
		}

		this.jsonObjectResult = resultObj.getResultMap();
		return "jsonObject";
	}




	public String removeMenuNode(){
		Result resultObj = new Result();
		try{
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();
			A2UserDto userDto = (A2UserDto)session.getAttribute("magSystemDtoSession");
			long userId = userDto.getId();
			resultObj = menuService.removeMenuById(Long.parseLong(id));
		}catch (Exception e) {
			resultObj.fault("出错:"+e.getLocalizedMessage());
			e.printStackTrace();
		}

		this.jsonObjectResult = resultObj.getResultMap();
		return "jsonObject";
	}

	/**
	 * 重新生成源码
	 * @return
	 */
	public String recoding(){
		Result resultObj = new Result();
		try{
			List<String> keyRandomList = new ArrayList<String>();
			keyRandomList.add(keyRandom);
			FreemarkerGen freemarkerGen = new FreemarkerGen();
			freemarkerGen.execute(keyRandomList,Integer.parseInt(sourceType));
			resultObj.success();
		}catch (Exception e) {
			resultObj.fault("出错:"+e.getLocalizedMessage());
			e.printStackTrace();
		}
		this.jsonObjectResult = resultObj.getResultMap();
		return "jsonObject";
	}





	public Logger getLog() {
		return log;
	}

	public void setLog(Logger log) {
		this.log = log;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public String getHasDataLimit() {
		return hasDataLimit;
	}

	public void setHasDataLimit(String hasDataLimit) {
		this.hasDataLimit = hasDataLimit;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOptType() {
		return optType;
	}

	public void setOptType(String optType) {
		this.optType = optType;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getRoleJsonArray() {
		return roleJsonArray;
	}

	public void setRoleJsonArray(String roleJsonArray) {
		this.roleJsonArray = roleJsonArray;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getSelectedRoleNodes() {
		return selectedRoleNodes;
	}

	public void setSelectedRoleNodes(String selectedRoleNodes) {
		this.selectedRoleNodes = selectedRoleNodes;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public List<Map<String, Object>> getJsonArrayResult() {
		return jsonArrayResult;
	}

	public void setJsonArrayResult(List<Map<String, Object>> jsonArrayResult) {
		this.jsonArrayResult = jsonArrayResult;
	}

	public Map<String, Object> getJsonObjectResult() {
		return jsonObjectResult;
	}

	public void setJsonObjectResult(Map<String, Object> jsonObjectResult) {
		this.jsonObjectResult = jsonObjectResult;
	}

	public IOperationlogService getOperationlogService() {
		return operationlogService;
	}

	public void setOperationlogService(IOperationlogService operationlogService) {
		this.operationlogService = operationlogService;
	}

	public IMenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(IMenuService menuService) {
		this.menuService = menuService;
	}

	public A1OperationlogDto getOprDtoBack() {
		return oprDtoBack;
	}

	public void setOprDtoBack(A1OperationlogDto oprDtoBack) {
		this.oprDtoBack = oprDtoBack;
	}

	public String getIconClass() {
		return iconClass;
	}

	public void setIconClass(String iconClass) {
		this.iconClass = iconClass;
	}

	public String getKeyRandom() {
		return keyRandom;
	}

	public void setKeyRandom(String keyRandom) {
		this.keyRandom = keyRandom;
	}

	public String getSourceType() {
		return sourceType;
	}

	public void setSourceType(String sourceType) {
		this.sourceType = sourceType;
	}
}
